package com.cleaningcloud.cld.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cleaningcloud.cld.domain.CldAccountInfo;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.cld.api.domain.CldProjectAccount;
import com.cleaningcloud.cld.service.ICldProjectAccountService;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.page.TableDataInfo;

/**
 * 项目账号信息Controller
 * 
 * @author cleaningcloud
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/account")
public class CldProjectAccountController extends BaseController
{
    @Autowired
    private ICldProjectAccountService cldProjectAccountService;

    /**
     * 查询项目账号信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CldProjectAccount cldProjectAccount)
    {
        startPage();
        List<CldProjectAccount> list = cldProjectAccountService.selectCldProjectAccountList(cldProjectAccount);
        return getDataTable(list);
    }

    /**
     * 根据项目ID查询项目账号信息列表
     */
    @GetMapping("/getProjectAccountListByProjectId/{projectId}")
    public AjaxResult getProjectAccountListByProjectId(@PathVariable("projectId") Long projectId)
    {
        return success(cldProjectAccountService.selectCldProjectAccountListByProjectId(projectId));
    }

    /**
     * 导出项目账号信息列表
     */
    @Log(title = "项目账号信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CldProjectAccount cldProjectAccount)
    {
        List<CldProjectAccount> list = cldProjectAccountService.selectCldProjectAccountList(cldProjectAccount);
        ExcelUtil<CldProjectAccount> util = new ExcelUtil<CldProjectAccount>(CldProjectAccount.class);
        util.exportExcel(response, list, "项目账号信息数据");
    }

    /**
     * 获取项目账号信息详细信息
     */
    @GetMapping(value = "/{projectAccountId}")
    public AjaxResult getInfo(@PathVariable("projectAccountId") Long projectAccountId)
    {
        return success(cldProjectAccountService.selectCldProjectAccountByProjectAccountId(projectAccountId));
    }

    /**
     * 新增项目账号信息
     */
    @Log(title = "项目账号信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CldProjectAccount cldProjectAccount)
    {
        if(!cldProjectAccountService.checkUserPhoneUnique(cldProjectAccount)){
            return error("保存客户失败，该手机号已存在");
        }
        return toAjax(cldProjectAccountService.insertCldProjectAccount(cldProjectAccount));
    }

    /**
     * 修改项目账号信息
     */
    @Log(title = "项目账号信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CldProjectAccount cldProjectAccount)
    {
        if(!cldProjectAccountService.checkUserPhoneUnique(cldProjectAccount)){
            return error("修改客户失败，该手机号已存在");
        }
        return toAjax(cldProjectAccountService.updateCldProjectAccount(cldProjectAccount));
    }


    /**
     * 重置密码
     */
    @Log(title = "项目账号信息", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody CldProjectAccount cldProjectAccount)
    {
        if(StringUtils.isEmpty(cldProjectAccount.getPassWord())){
            cldProjectAccount.setPassWord("123456");
        }
        cldProjectAccount.setPassWord(SecurityUtils.encryptPassword(cldProjectAccount.getPassWord()));
        cldProjectAccount.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(cldProjectAccountService.resetPwd(cldProjectAccount));
    }

    /**
     * 删除项目账号信息
     */
    @Log(title = "项目账号信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectAccountIds}")
    public AjaxResult remove(@PathVariable Long[] projectAccountIds)
    {
        return toAjax(cldProjectAccountService.deleteCldProjectAccountByProjectAccountIds(projectAccountIds));
    }
}
